package com.chang.bootplus.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chang.bootplus.base.exception.BusException;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.*;
import com.chang.bootplus.po.*;
import com.chang.bootplus.service.UserDepartService;
import com.chang.bootplus.vo.UserDepartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"部门管理"})
@RequestMapping("/department")
@RestController
public class AdminDepartController {

    @Autowired
    UserDepartService userDepartService;


    @PostMapping("/addDepart")
    @ApiOperation(httpMethod = "POST", value = "添加部门")
    public ApiResponse<Void> addRole(@RequestBody UserDepartAddPO po){
        if (po.getParentId()==null||po.getDepartment()==null){
            throw new BusException("字段属性为空");
        }
        UserDepart one = userDepartService.getOne(new LambdaQueryWrapper<UserDepart>().eq(UserDepart::getDepartment, po.getDepartment()));
        if (one!=null){
            throw new BusException("部门已存在");
        }
        UserDepart userDepart = BeanUtil.beanA2beanB(po, UserDepart.class);
        userDepartService.save(userDepart);
        return new ApiResponse().setReMsg("添加成功");
    }


    @PostMapping("/deleteDepart")
    @ApiOperation(httpMethod = "POST", value = "删除部门")
    public ApiResponse<UserDepart> deleteRole(@RequestBody DepartPO po){
        boolean b = userDepartService.removeById(po.getId());
        if (!b){
            throw new BusException("部门不存在");
        }
        return new ApiResponse().setReMsg("删除成功");
    }


    @PostMapping("/selectDepart")
    @ApiOperation(httpMethod = "POST", value = "查询部门")
    public ApiResponse<UserDepartVO> selectRole(@RequestBody UserDepartPO po){
        List<UserDepart> records = userDepartService.list(new LambdaQueryWrapper<UserDepart>().like(UserDepart::getDepartment,po.getDepartment()));
        return new ApiResponse(records);
    }
    @PostMapping("/listDepart")
    @ApiOperation(httpMethod = "POST", value = "部门列表")
    public ApiResponse<UserDepartVO> listRole(){
        List<UserDepartVO> records = userDepartService.listDepart();
        return new ApiResponse(records);
    }



    @PostMapping("/updateDepart")
    @ApiOperation(httpMethod = "POST", value = "更新部门")
    public ApiResponse<Void> updateRole(@RequestBody UserDepart po){
        UserDepart byId = userDepartService.getById(po.getId());
        if (byId==null){
            throw new BusException("更新失败,部门不存在");
        }
        //更新
        boolean b = userDepartService.updateById(BeanUtil.beanA2beanB(po,UserDepart.class));
        return new ApiResponse().setReMsg("更新成功");
    }




}
